package com.snmp.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.snmp.test.entity.WirelessAlarmData;
import com.snmp.test.snmp.SnmpSendUtils;
import com.snmp.test.snmp.SnmpUtilSendTrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.event.ResponseEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * @Author: WangChao
 * @Description: TODO
 * @Date: 2019/6/18 9:41
 */
@RestController
@RequestMapping("snmp")
public class SnmpController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SnmpController.class);

    @Autowired
    private SnmpUtilSendTrap snmpUtilSendTrap;

    @RequestMapping("/sendSnmp")
    public String sendSnmp(@RequestBody WirelessAlarmData req) {
        LOGGER.info("请求参数：{}",req.toString());
        LOGGER.info("SnmpUtilSendTrap...start...");
        try {
            snmpUtilSendTrap.initComm();
            snmpUtilSendTrap.sendPDU(req);
        } catch (IOException e) {
            LOGGER.error("发送snmpTrap失败：{}",e.getMessage());
            return "发送失败："+e.getMessage();
        }
        LOGGER.info("SnmpUtilSendTrap...end...");

        return "发送成功";
    }

    @RequestMapping("/send")
    public String sendSnmpWithoutReq() {
        LOGGER.info("SnmpUtilSendTrap...start...");
        WirelessAlarmData req = new WirelessAlarmData();
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowString = sdf.format(now);
        req.setAckTime(nowString);
        req.setOccurTime(nowString);
        req.setNeType("无线专网");
        req.setAlarmLevel(3);
        req.setAlarmSpecificproblems("设备故障");
        req.setNeIpAddr("10.8.170.129");
        req.setAdditionalInfo("12");
        req.setAlarmCategory("12");
        req.setAlarmConfirm(1);
        req.setAlarmCsn("233");
        req.setAlarmOperator("李四");
        req.setAlarmProbablecause("hahhahhah");
        req.setAlarmProposedrepairactions("233334");
        req.setAlarmRestore(3);
        req.setAlarmType(23);
        req.setAlId("2222");
        req.setClearCategory("54545");
        req.setClearOperator("张三");
        req.setCreateTime(now);
        req.setUpdateTime(now);
        req.setServiceAffectFlag("123231231");
        req.setRestoreTime(nowString);
        req.setProductId(232);
        req.setObjectInstanceType("445");
        req.setNeDevId("4532");
        req.setMoName("jsjfef");
        req.setId(23);
        req.setExtendInfo("wefrwe");
        req.setDevCsn("ewe");
        req.setClearType("2323");

        String jsonString = JSONObject.toJSONString(req);
        LOGGER.info(jsonString);
        try {
            snmpUtilSendTrap.initComm();
            snmpUtilSendTrap.sendPDU(req);
        } catch (IOException e) {
            LOGGER.error("发送snmpTrap失败：{}",e.getMessage());
            return "发送失败："+e.getMessage();
        }
        LOGGER.info("SnmpUtilSendTrap...end...");

        return "发送成功";
    }

    @GetMapping("/get")
    public Object getSnmpResponse(@RequestParam String community, @RequestParam String oid) throws IOException {
        SnmpSendUtils util = new SnmpSendUtils();
        util.initComm("192.168.2.1", "161");
        HashMap<String, Object> eventMap = util.sendPDU(community, oid);
        return eventMap;
    }
}
